<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 头像 -->
            <div class="avatar_box">
                <img src="../../assets/1.gif" alt="">
            </div>
            <!-- 登录表单区域 -->
            <el-form  class="login_form">
                <!-- 用户名 -->
                <el-form-item>
                    <el-input   prefix-icon="el-icon-user-solid" v-model="userName"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item >
                    <el-input  prefix-icon="el-icon-s-goods" v-model="passWord"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                   <el-button type="primary" @click="onSubmit">登录</el-button>
                   <el-button type="success" @click="register">注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>  
import { setToken, setUserName } from "../../utils/auth";
import request from "../../utils/request";
export default {
  name: "login",
  data() {
    return {
      userName: "admin",
      passWord: "password"
    };
  },
  methods: {
    onSubmit() {
      var userName = this.userName;
      var passWord = this.passWord;
      request({
        url: "/api/user/login",
        method: "post",
        data: {
          userName,
          passWord
        }
      }).then(res => {
          console.log(res)
        if (res.msg === "登录成功") {
          this.$message({
            message: "恭喜你，登录成功",
            type: "success"
          });
          setToken(res.token);
          setUserName(res.userName);
          
          this.$router.push({path:"/",query:{name:res.userName}});
        }
      });
    },
    register(){
        this.$router.push('/register')
    }
  }
};
</script>
<style lang="less" scoped>
.login_container{
    background-color:lightseagreen;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 300px;
    background-color:#fff;
    border-radius:3px;
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
    .avatar_box{
        height: 130px;
        width: 130px;
        border: 1px solid #eee;
        border-radius:50%;
        padding:10px;
        box-shadow:0 0  10px #ddd;
        position:absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img{
            width:100%;
            height: 100%;
            border-radius: 50%;
            background-color:#eee;
        }
    }
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
.btns{
    display: flex;
    justify-content:center;
}
</style>